<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Echo WebSocket测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        #messageLog {
            height: 300px;
            border: 1px solid #ccc;
            margin: 20px 0;
            padding: 10px;
            overflow-y: auto;
            font-family: monospace;
        }
        button {
            padding: 8px 15px;
            margin-right: 10px;
        }
        input {
            padding: 8px;
            width: 300px;
        }
        .success {
            color: green;
        }
        .error {
            color: red;
        }
        .info {
            color: blue;
        }
    </style>
</head>
<body>
    <h1>Echo WebSocket测试</h1>
    <p>这是一个简单的Echo服务器测试，所有发送的消息会直接返回。</p>
    
    <div>
        <button id="connectBtn">连接</button>
        <button id="disconnectBtn" disabled>断开</button>
    </div>
    
    <div id="messageLog"></div>
    
    <div>
        <input type="text" id="messageInput" placeholder="输入要发送的消息" disabled>
        <button id="sendBtn" disabled>发送</button>
    </div>
    
    <script>
        const connectBtn = document.getElementById('connectBtn');
        const disconnectBtn = document.getElementById('disconnectBtn');
        const messageInput = document.getElementById('messageInput');
        const sendBtn = document.getElementById('sendBtn');
        const messageLog = document.getElementById('messageLog');
        
        let socket = null;
        
        function log(message, className) {
            const msgElement = document.createElement('div');
            msgElement.className = className || '';
            msgElement.textContent = message;
            messageLog.appendChild(msgElement);
            messageLog.scrollTop = messageLog.scrollHeight;
        }
        
        connectBtn.addEventListener('click', function() {
            log('正在连接...', 'info');
            
            try {
                // 使用绝对路径确保连接正确
                const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
                const wsUrl = `${wsProtocol}//${window.location.host}/echo`;
                
                log(`连接到: ${wsUrl}`, 'info');
                socket = new WebSocket(wsUrl);
                
                socket.onopen = function() {
                    log('连接成功！', 'success');
                    connectBtn.disabled = true;
                    disconnectBtn.disabled = false;
                    messageInput.disabled = false;
                    sendBtn.disabled = false;
                };
                
                socket.onmessage = function(event) {
                    log(`收到: ${event.data}`, 'success');
                };
                
                socket.onclose = function(event) {
                    log(`连接关闭，代码: ${event.code}`, 'info');
                    
                    if (event.code === 1006) {
                        log('异常关闭 - 可能是服务器未运行或拒绝连接', 'error');
                    }
                    
                    connectBtn.disabled = false;
                    disconnectBtn.disabled = true;
                    messageInput.disabled = true;
                    sendBtn.disabled = true;
                    socket = null;
                };
                
                socket.onerror = function() {
                    log('连接错误', 'error');
                };
            } catch (e) {
                log(`创建WebSocket连接失败: ${e.message}`, 'error');
            }
        });
        
        disconnectBtn.addEventListener('click', function() {
            if (socket) {
                log('正在断开连接...', 'info');
                socket.close();
            }
        });
        
        sendBtn.addEventListener('click', function() {
            sendMessage();
        });
        
        messageInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });
        
        function sendMessage() {
            if (!socket || socket.readyState !== WebSocket.OPEN) {
                log('未连接，无法发送消息', 'error');
                return;
            }
            
            const message = messageInput.value.trim();
            if (message === '') return;
            
            log(`发送: ${message}`, 'info');
            socket.send(message);
            messageInput.value = '';
        }
    </script>
</body>
</html> 